﻿using System;
using System.Data;
using System.Web.Security;
using System.Web.UI.WebControls;
using wsRK.AppUser;
using wsRK.Web;

namespace nkstdgia.main.membership
{
    public partial class user : WebPage
    {
        public bool blnIsAdministrator
        {
            get
            {
                return User.IsInRole("administrator");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindData();
            }
        }

        private void bindData()
        {
            DataTable table = new DataTable();
            table.Columns.Add("UserId");
            table.Columns.Add("Username");
            table.Columns.Add("Email");
            table.Columns.Add("Roles");
            table.Columns.Add("LastActivityDate");
            table.Columns.Add("Status");

            MembershipUserCollection collection = MembershipDataservice.getAllUsers();
            foreach (MembershipUser item in collection)
            {
                string UserId = item.ProviderUserKey.ToString();
                string UserName = item.UserName;
                string Email = item.Email;
                string RolesList = "";
                MembershipUser obj = MembershipDataservice.getUser(item.UserName);

                string[] listroles = RoleDataservice.getRolesForUser(obj.UserName);
                if (blnIsAdministrator)
                {
                    for (int i = 0; i < listroles.Length; i++)
                    {
                        RolesList += listroles[i] + " , ";
                    }
                    if (RolesList.Length >= 3)
                    {
                        RolesList = RolesList.Remove(RolesList.Length - 3);
                    }

                    string LastActivityDate = item.LastActivityDate.ToString();
                    table.Rows.Add(UserId, UserName, Email, RolesList, LastActivityDate, obj.IsApproved);
                }
                else
                {
                    bool haveAdmin = false;
                    for (int i = 0; i < listroles.Length; i++)
                    {
                        if (listroles[i] == "administrator")
                        {
                            haveAdmin = true;
                            break;
                        }
                        RolesList += listroles[i] + " , ";
                    }
                    if (!haveAdmin)
                    {
                        if (RolesList.Length >= 3)
                        {
                            RolesList = RolesList.Remove(RolesList.Length - 3);
                        }

                        string LastActivityDate = item.LastActivityDate.ToString();
                        table.Rows.Add(UserId, UserName, Email, RolesList, LastActivityDate, obj.IsApproved);
                    }
                }
            }

            lvUserList.DataSource = table;
            lvUserList.DataBind();
        }

        protected void Active_Inactive_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbx = (CheckBox)sender;
            Label lbl = (Label)cbx.Parent.FindControl("UsrID");
            MembershipUser obj = MembershipDataservice.getUser(new Guid(lbl.Text));
            if (obj.IsApproved == true)
            { obj.IsApproved = false; }
            else
            { obj.IsApproved = true; }
            Membership.UpdateUser(obj);
            bindData();
        }

        protected void Reset_Password_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            string strUsetID4ResetPassword = lbtn.CommandArgument.ToString();
            Response.Redirect("userchangepassword.aspx?uid=" + strUsetID4ResetPassword);
        }

        protected void lbtnAddUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("createuser.aspx");
        }

        protected void lvUserList_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteUser")
            {
                string strUserId = e.CommandArgument.ToString();
                MembershipUser obj = MembershipDataservice.getUser(new Guid(strUserId));
                MembershipDataservice.deleteUser(obj.UserName);
                this.bindData();
                base.showMasterMessage("Xóa thành công!");
            }
        }

        protected void lvUserList_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (!blnIsAdministrator && !User.IsInRole("Quản trị viên"))
            {
                LinkButton btnDeleteUser = e.Item.FindControl("btnDeleteUser") as LinkButton;
                if (btnDeleteUser != null)
                {
                    btnDeleteUser.Visible = false;
                }
            }
        }
    }
}